Python Function

A function is a block of reusable code that is used to perform a specific action. It take inputs, do some specific computation and produces output.The advantages of using functions are:

  • Reducing duplication of code
  • Decomposing complex problems into simpler pieces
  • Improving clarity of the code
  • Reuse of code
  • Information hiding
  • user-defined functions
A function is a block of code that performs a specific task. Suppose, you need to create a program to create a circle and color it. You can create two functions to solve this problem:
  • create a circle function
  • create a color function
Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.

Python Function Types
  1. built-in functions
  2. user defined functions
Creating a Function
A function is created with def keyword
def myfunc():
print("Hello python");
 Above shown is a function definition that consists of the following components.
  • Keyword def that marks the start of the function header.
  • A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python.
  • Parameters (arguments) through which we pass values to a function. They are optional.
  • A colon (:) to mark the end of the function header.
  • Optional documentation string (docstring) to describe what the function does.
  • One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).
  • An optional return statement to return a value from the function.
Calling Function
The function is later executed when needed. We say that we call the function. If we call a function, the statements inside the function body are executed. They are not executed until the function is called.
myfunc()
Where to define function
Functions can be defined inside a module, a class, or another function. Function defined inside a class is called a method.

Python return keyword
def show_message(msg):
print(msg)
def cube(x):
return x * x * x
x = cube(3)
print(x)
show_message("Computation finished.")
print(show_message("Ready."))
The return Statement
The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.

Syntax: return [expression_list] 

It can contain the expression which gets evaluated and value is returned to the caller function. If the return statement has no expression or does not exist itself in the function then it returns the None object.
# Defining function  
def sum():
a = 10
b = 20
c = a + b
return c
# calling sum() function in print statement
print("The sum is:", sum())
OutPut:
The sum is: 30
  
Create Function without return statement
# Defining function  
def sum():
a = 10
b = 20
c = a + b
# calling sum() function in print statement
print(sum())
OutPut:
None

No comments:

Post a Comment